﻿using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractSrm;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Service.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace HamcoWcs.Service.Srm.SrmTaskFinders
{
    [AutoInject(typeof(ISrmTaskFinderFactory), InjectType.Scope)]
    public class SrmTaskFinderFactory : ISrmTaskFinderFactory
    {
        public IEnumerable<ISrmTaskFinder> TaskFinders => _taskFinders;
        private readonly IEnumerable<ISrmTaskFinder> _taskFinders;
        protected readonly IBaseConfigService _baseConfigService;


        public SrmTaskFinderFactory(IEnumerable<ISrmTaskFinder> taskFinders, IBaseConfigService baseConfigService)
        {
            _taskFinders = taskFinders;
            _baseConfigService = baseConfigService;
        }

        public ISrmTaskFinder GetFinder()
        {
            string policy = "";
            try
            {
                policy = _baseConfigService.GetConfigValue("SRM_TASK_FIND_POLICY");
            }
            catch (Exception)
            {
            }

            if (string.IsNullOrEmpty(policy)) policy = "Default";
            //Console.WriteLine($"Current Policy => {policy}");
            Debug.WriteLine($"Current Policy => {policy}");
            return _taskFinders.FirstOrDefault(x => x.Policy.Equals(policy, StringComparison.OrdinalIgnoreCase));
        }
    }
}
